{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3560a64c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !wget https://github.com/mesolitica/malaysian-dataset/raw/master/translation/flores200-eval/bjn_Latn.dev\n",
    "# !wget https://github.com/mesolitica/malaysian-dataset/raw/master/translation/flores200-eval/eng_Latn.dev\n",
    "# !wget https://github.com/mesolitica/malaysian-dataset/raw/master/translation/flores200-eval/ind_Latn.dev\n",
    "# !wget https://github.com/mesolitica/malaysian-dataset/raw/master/translation/flores200-eval/jav_Latn.dev\n",
    "# !wget https://github.com/mesolitica/malaysian-dataset/raw/master/translation/flores200-eval/zsm_Latn.dev\n",
    "# !wget https://github.com/mesolitica/malaysian-dataset/raw/master/translation/flores200-eval/zho_Hans.dev\n",
    "# !wget https://github.com/mesolitica/malaysian-dataset/raw/master/translation/flores200-eval/tam_Taml.dev"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2d713f77",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('ind_Latn.dev') as fopen:\n",
    "    en = fopen.read().split('\\n')\n",
    "    \n",
    "with open('zsm_Latn.dev') as fopen:\n",
    "    ms = fopen.read().split('\\n')\n",
    "    \n",
    "en_, ms_ = [], []\n",
    "for i in range(len(en)):\n",
    "    if len(en[i]) and len(ms[i]):\n",
    "        en_.append(en[i])\n",
    "        ms_.append(ms[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "076a2a37",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(997, 997)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(en_), len(ms_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "14d402ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm import tqdm\n",
    "import requests\n",
    "import os\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4795de0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "!mkdir base-ind-ms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "37ac8ece",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n"
     ]
    }
   ],
   "source": [
    "from transformers import AutoTokenizer, T5ForConditionalGeneration\n",
    "\n",
    "tokenizer = AutoTokenizer.from_pretrained('mesolitica/nanot5-base-malaysian-translation-v2')\n",
    "model = T5ForConditionalGeneration.from_pretrained('mesolitica/nanot5-base-malaysian-translation-v2').cuda()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3274b3a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "all_special_ids = [0, 1, 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4ed56e3f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████████████████████████████████████████████████████████████████| 997/997 [04:03<00:00,  4.09it/s]\n"
     ]
    }
   ],
   "source": [
    "for i in tqdm(range(len(ms_))):\n",
    "    filename = os.path.join('base-ind-ms', f'{i}.json')\n",
    "    \n",
    "    if os.path.exists(filename):\n",
    "        continue\n",
    "    \n",
    "    headers = {\n",
    "        'accept': 'application/json',\n",
    "        'Content-Type': 'application/json',\n",
    "    }\n",
    "    \n",
    "    input_ids = tokenizer.encode(f'terjemah ke Melayu: {en_[i]}{tokenizer.eos_token}', return_tensors = 'pt')\n",
    "    outputs = model.generate(input_ids.cuda(), max_length = 1024, num_beams=5, early_stopping=True)\n",
    "    outputs = [i for i in outputs[0] if i not in all_special_ids]\n",
    "    r = tokenizer.decode(outputs, spaces_between_special_tokens = False).strip()\n",
    "\n",
    "    with open(filename, 'w') as fopen:\n",
    "        json.dump({'text': ms_[i], 'r': r}, fopen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "73e0fe41",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sacrebleu.metrics import BLEU, CHRF, TER\n",
    "\n",
    "chrf = CHRF(word_order = 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2a764b8d",
   "metadata": {},
   "outputs": [],
   "source": [
    "predicted = []\n",
    "for i in range(len(ms_)):\n",
    "    filename = os.path.join('base-ind-ms', f'{i}.json')\n",
    "    with open(filename) as fopen:\n",
    "        d = json.load(fopen)\n",
    "    predicted.append(d['r'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f1772a2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "chrF2++ = 58.62"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score = chrf.corpus_score(predicted, [ms_])\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01f24ac6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
